package sgi.client;

import java.io.IOException;

import sgi.message.MessageComplaint;
import sgi.user.User;

public class SGIComplaintController extends SGIController {

	public static enum ComplaintType {SERVICES,CONTENT,FINANCIAL,OTHER};
	public static SGIComplaintController complaint;
	public static SGIComplaintUI complaintUI;
	private static User user;
	
	public static SGIComplaintController create(User user) throws IOException
	{
		if(SGIComplaintController.complaint == null)
		{
			SGIComplaintController.complaint = new SGIComplaintController(user);
			complaintUI.setComplaintController(SGIComplaintController.complaint);
		}
		
		SGIComplaintController.complaintUI.setVisible(true);
		return SGIComplaintController.complaint;
	}
	
	public static void terminate()
	{
		SGIComplaintController.complaintUI.setVisible(false);
		SGIComplaintController.complaint=null;
		SGIComplaintController.complaintUI=null;
	}
	
	private SGIComplaintController(User user) 
	{
		SGIComplaintController.complaintUI = new SGIComplaintUI(user);
		SGIComplaintController.user = new User(user);  
	}
	
	public void submitComplaint(int type,String reason,String description)
	{
		MessageComplaint msg = new MessageComplaint(SGIComplaintController.user,type,reason,description);
		try {
				SGIClientConnectionController conn = SGIClientConnectionController.createConnection();
				conn.handleMessageFromClientUI(msg);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	@Override
	void handleMessage(Object msg) {
		if(((MessageComplaint)msg).isReceived() == true)
			complaintUI.notify("Complaint received.");
		else
			complaintUI.notify("Complaint not received.");
	}
	
	public static SGIComplaintUI getUI()
	{
		return complaintUI;
	}
}
